<?php
namespace Common\Model;

/**
 * 城市资源表
 * @modify trainyao 2015-12-31 创建
 * @index city(`province_id`)
 */

Class CityModel extends \Common\Model\BaseModel
{
    protected $trueTableName = 'bk_city';
    
    /**
     * 返回城市名称
     * @param integer $cityId 城市ID
     * @return type
     */
    public function getName($cityId) {
        return $this->where(array('id' => $cityId))->getField('name');
    }
    
    /**
     * 获取城市数据
     * @modify trainyao 2015-12-31  创建
     * @param  integer   $id 城市id
     * @return array
     */
    public function getInfo($id = null) {
        $option = array();
        if (!empty($id)) {
            $option['id'] = $id;
        }
        $option['isdelete'] = NOT_DELETE;
        
        $fields = 'id,province_id,name';
        $data = array();
        
        if (!empty($id)) {
            $data = $this->where($option)->field($fields)->find();
        }
        $data = $this->where($option)->field($fields)->select();
        
        if (empty($data)) {
            return array();
        }
        return $data;
    }
    
    /**
     * 根据省份取城市
     * @modify trainyao 2015-12-31 创建
     * @param  integer   $provinceId 省份id
     * @return array
     */
    public function getInfoByProvince($provinceId) {
        if (is_array($provinceId)) {
            $option['province_id'] = array('in', $provinceId);
        } else {
            $option['province_id'] = $provinceId;
        }
        $option['isdelete'] = NOT_DELETE;
        // $option = array('province_id' => $provinceId, 'isdelete' => NOT_DELETE);
        $fields = 'id,province_id,name';
        
        $data = $this->where($option)->field($fields)->select();
        
        if (empty($data)) {
            return array();
        }
        return $data;
    }
    
    /**
     * 增加城市
     * @modify trainyao 2015-12-31 创建
     * @param  string   $provinceId      省份id
     * @param  string   $name      城市名
     * @return boolean 执行结果
     */
    public function addInfo($provinceId, $name) {
        $data = array('province_id' => $provinceId, 'name' => $name);
        return $this->add($data) === false ? false : true;
    }
}
